﻿Imports System.Data.SqlClient

Public Class frm_customerEditor

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()

    End Sub
    Public Sub LoadData()
        Dim cn As New SqlConnection("SERVER = .; USER = sa; PASSWORD = 123; DATABASE = HOTELAPP;")
        cn.Open()
        Dim cmd As New SqlCommand("SELECT * FROM TblCustomers")

        Dim reader = cmd.ExecuteReader()
        Dim dt As New DataTable
        dt.Load(reader)
        reader.Close()

    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim cn As New SqlConnection("server = .; user = sa; password = 123 ; database = HOTELAPP")
        cn.Open()
        'create new one
        Dim cmds As New SqlCommand("SELECT * FROM TblCities", cn)
        Dim drs = cmds.ExecuteReader()
        Dim dts As New DataTable
        dts.Load(drs)
        drs.Close()

        Me.txt_CustCity.ValueMember = "CityCode"
        Me.txt_CustCity.DisplayMember = "CityName"
        Me.txt_CustCity.DataSource = dts
        Me.txt_CustCity.SelectedIndex = -1
        'create new more
        Dim cmdd As New SqlCommand("SELECT * FROM TblCountries", cn)
        Dim drd = cmdd.ExecuteReader()
        Dim dtd As New DataTable
        dtd.Load(drd)
        drd.Close()

        Me.txt_CustCountry.DisplayMember = "CountryName"
        Me.txt_CustCountry.ValueMember = "CountryCode"
        Me.txt_CustCountry.DataSource = dtd

    End Sub

    Private Sub btn_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cancel.Click

        If txt_CustID.Text = "" Then
            Dim cn As New SqlConnection("server =.; user = sa; password = 123; database = HOTELAPP")
            cn.Open()

            'MessageBox.Show("connected")
            Dim cmd As New SqlCommand()
            cmd.Connection = cn
            cmd.CommandText = "INSERT INTO TblCustomers VALUES(N'" & txt_CustFullName.Text & "', N'" & txt_CustFname.Text & "', N'" & txt_CustLname.Text & "', N'" & txt_CobSex.Text & "', N'" & DateString & "', N'" & txt_CustPhone.Text & "', N'" & txt_CustEmail.Text & "', N'" & txt_CustPassNo.Text & "', N'" & txt_CustCity.SelectedValue & "', N'" & txt_CustCountry.SelectedValue & "', N'" & txt_CustPax.Text & "', N'" & txt_CustAdult.Text & "', N'" & txt_CustInfant.Text & "', N'" & txt_CustCom.Text & "' ) "
            cmd.ExecuteNonQuery()
            'MessageBox.Show("OK")

        Else
            Dim cn As New SqlConnection("server =.; user = sa; password = 123; database = HOTELAPP")
            cn.Open()

            'MessageBox.Show("connected")
            Dim cmdup As New SqlCommand()
            cmdup.Connection = cn
            cmdup.CommandText = "UPDATE TblCustomers  SET CustomerFullName = N'" & txt_CustFullName.Text & "', CustomerFirstName = N'" & txt_CustFname.Text & "', CustomerLastName = N'" & txt_CustLname.Text & "', CustomerSex = N'" & txt_CobSex.SelectedValue & "', CustomerDob = N'" & DateString & "', CustomerPhone = N'" & txt_CustPhone.Text & "', CustomerEmail = N'" & txt_CustEmail.Text & "', CustomerCities = N'" & txt_CustCity.SelectedValue & "', CustomerCountries = N'" & txt_CustCountry.SelectedValue & "', CustomerPax =  N'" & txt_CustPax.Text & "', CustomerAdult = N'" & txt_CustAdult.Text & "', CustomerInfant = N'" & txt_CustInfant.Text & "', CustomerAdr = N'" & txt_CustCom.Text & "' WHERE CustomerID = N'" & txt_CustID.Text & "' "
            cmdup.ExecuteNonQuery()

        End If
        Me.DialogResult = Windows.Forms.DialogResult.OK

    End Sub
End Class



